package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.MyTravelTable;
import cn.wolfcode.trip.base.mapper.MyTravelTableMapper;
import cn.wolfcode.trip.base.query.MyTravelTableQueryObject;
import cn.wolfcode.trip.base.service.IMyTravelTableService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MyTravelTableServiceImpl implements IMyTravelTableService {
    @Autowired
    private MyTravelTableMapper  myTravelTableMapper;

    /**
     * 检查当前用户是否将攻略加入旅行单
     * @param strategyId
     * @param userId
     * @return
     */
    @Override
    public boolean selectTableRecord(Long strategyId,Long userId) {
        List<MyTravelTable> list = myTravelTableMapper.selectListByStrategy(userId,null);
        for (MyTravelTable ele : list) {
            if(ele.getStrategy().getId() == strategyId){
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 保存当前攻略到旅行单中
     * @param  myTravelTable
     */
    @Override
    public void save(MyTravelTable myTravelTable) {
        myTravelTableMapper.insert(myTravelTable);
    }

    /**
     * 删除当前旅行单
     * @param userId
     * @param strategyId
     */
    @Override
    public void deleteMyTravelTable(MyTravelTable myTravelTable) {
        myTravelTableMapper.deleteRelation(myTravelTable);
    }

    /**
     * 查询我的旅行单的列表操作
     * @param qo
     * @return
     */
    @Override
    public PageInfo query(MyTravelTableQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<MyTravelTable> list = myTravelTableMapper.selectListByStrategy(qo.getUserId(),qo.getTravelStatus());
        return new PageInfo(list);

    }

    @Override
    public int selectTravelCount(MyTravelTableQueryObject qo) {
        int result = myTravelTableMapper.selectTravelCount(qo.getUserId(),qo.getTravelStatus());
        return result;
    }

    @Override
    public void deleteMyTravelTableById(Long id) {
        myTravelTableMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateMyTravelTableById(Long id, Long travelStatus) {
        myTravelTableMapper.updateByTravelStatus(id,travelStatus);
    }

}
